GitHub ActionsでSphinxのPDFをビルドする
前提
Sphinxのconf.py には、PDF出力の設定済み
環境変数で pdf タグを渡すと、conf.py内で良い感じにPDF出力用に分岐する
GitHub Actionsの定義
code:main.yml
name: org/reponame
on:
release:
types:
- created
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: build pdf
uses: docker://sphinxdoc/sphinx-latexpdf
with:
args: sphinx-build -M latexpdf source build
env:
LANG: C.UTF-8
SPHINXOPTS: -t pdf
- name: Archive PDF artifacts
uses: actions/upload-artifact@v2
with:
name: epp3ja.pdf
path: build/latex/epp3ja.pdf
uses: docker:// の書き方
conf.py
code:conf.py
import os
from datetime import datetime, timezone, timedelta
BASEDIR = os.path.dirname(os.path.abspath(__file__))
# -- Timestamp
JST = timezone(timedelta(hours=9), name='JST')
NOW = datetime.now(timezone.utc)
if 'pdf' in tags: # for: sphinx-build -t pdf
today_fmt = '%Y/%m/%d %H:%M:%S'
else:
today_fmt = '%Y/%m/%d %H'
today = NOW.astimezone(JST).strftime(today_fmt)
# -- Project information -----------------------------------------------------
project = 'エキスパートPythonプログラミング 改訂3版'
copyright = '2020, eppja ({})'.format(today)
author = 'eppja'
version = release = '3版 {}'.format(today)
# -- General configuration ---------------------------------------------------
language = 'ja'
numfig = True
pygments_style = 'sphinx'
# -- extensions
extensions = [
'sphinx.ext.todo',
]
if 'pdf' not in tags:
todo_include_todos = True
# -- html
html_theme = 'bizstyle'
html_logo = 'assets/cover.png'
html_sidebars = {
'**': [
'pageinfo.html',
'localtoc.html',
'relations.html',
'sourcelink.html',
'searchbox.html',
]
}
# -- pdf
latex_documents = [
('indexpdf', 'epp3ja.tex', project, author, 'manual', True),
]
latex_elements = {
'papersize': 'b5paper',
'figure_align': 'H',
'preamble': r'''
\geometry{top=1.5cm, bottom=1cm, left=1cm, right=1cm, includefoot}
\usepackage{titlesec}
\titleclass{\chapter}{top}
\titleformat{\chapter}
{\centering\normalfont\huge\bfseries} % format
{\titlerule5pt\vspace{3pt}\titlerule2pt\vspace{1em}} % laberl {0pt} % sep
{\vspace{3pt}\Huge} % before-code
% code
[{\vspace{1em}\titlerule2pt\vspace{3pt}\titlerule5pt}] % after-code \titleformat{\section}block {\normalfont\huge\bfseries} % format
{\thesection.} % label
{0.5em} % sep
{\vspace{3pt}\huge} % before-code
[{\vspace{2pt}\titlerule2pt}] % after-code \setcounter{secnumdepth}{2}
\setcounter{tocdepth}{2}
\usepackage{hyperref}
\usepackage{pxjahyper}
\hypersetup{bookmarksnumbered}
'''
}
latex_show_urls = 'footnote'
latex_use_xindy = True
latex_show_pagerefs = True
用紙サイズや余白サイズを調整して、1行の文字数が実際の書籍に近くなるようにしています。
おおよそのページ数や、コードの改行位置などが確認できます。
出力結果
https://scrapbox.io/files/5fa6701cc803db001e170e10.png